<?php
App::uses('AppController', 'Controller');

class PostsController extends AppController {
	public $uses = array();

	public function beforeFilter() {
		parent::beforeFilter();
		$this->Auth->allow('home', 'add', 'show', 'drawings', 'stories');
	}

	public function index() {
	
	}

	public function home() {
		$this->paginate = array(
			'conditions' => array(
				'Post.published ' => POST_PUBLISHED
			),
			'fields' => array(
				'Post.id', 'Post.title', 'Post.description', 'Post.image', 'Post.created'
			),
			'order'=> array(
				'Post.created'=> 'DESC'
			),
			'limit' => POSTS_PAGE_LIMIT,
		);
		$posts = $this->paginate('Post');

		$this->set('posts', $posts);
		$this->set('title_for_layout', __('Prečo práve ja?'));
	}

	public function stories() {
		$this->paginate = array(
			'conditions' => array(
				'Post.published ' => POST_PUBLISHED,
				'Post.category_id'=> POST_CATEGORY_STORY
			),
			'fields' => array(
				'Post.id', 'Post.title', 'Post.description', 'Post.image', 'Post.created'
			),
			'order'=> array(
				'Post.created'=> 'DESC'
			),
			'limit' => POSTS_PAGE_LIMIT,
		);
		$posts = $this->paginate('Post');

		$this->set('posts', $posts);
		$this->set('title_for_layout', __('Prečo práve ja? | Príbehy'));
	}

	/** Obrázky z hajzlov a tak Majo choď do riti :)  
	************************/
	public function drawings() {
		$this->paginate = array(
			'conditions' => array(
				'Post.published ' => POST_PUBLISHED,
				'Post.category_id'=> POST_CATEGORY_DRAWING
			),
			'fields' => array(
				'Post.id', 'Post.title', 'Post.description', 'Post.image', 'Post.created'
			),
			'order'=> array(
				'Post.created'=> 'DESC'
			),
			'limit' => POSTS_PAGE_LIMIT,
		);
		$posts = $this->paginate('Post');

		$this->set('posts', $posts);
		$this->set('title_for_layout', __('Prečo práve ja? | Nástenné umenie'));
	}

	public function show($id) {
		$id = (int) $id;

		$post = $this->Post->find('first', array(
			'conditions' => array(
				'Post.published ' => POST_PUBLISHED,
				'Post.id' => $id
			),
			'fields' => array(
				'id', 'title', 'description', 'image', 'created'
			),
		));
		//autoincrement
		$this->Post->updateAll(
			array(
			  'Post.views' => 'Post.views+1'
			), 
			array('Post.id' => $id)
		);
		if (empty($post)) {
			throw new NotFoundException(__('Stránka neexistuje'));
		}

		$this->set('post', $post);
		$this->set('title_for_layout', $post['Post']['title'] . ' | ' .  __('Prečo práve ja?'));
	}

	public function add() {
		if (!$this->request->is('ajax')) {
			exit;
		}

		if ($this->request->is('post') || $this->request->is('put')) {
			$this->Post->set($this->request->data);
			$this->request->data['Post']['published'] = POST_PUBLISHED;
			$this->request->data['Post']['ip_address'] = $_SERVER['REMOTE_ADDR'];

			$accessValidation = $this->Post->validateAccess();

			if (!$accessValidation) {
				$this->set('formMessage', __('Oddýchni si trochu a nebuď tak premotivovaný.'));
			}
			else if ($this->Post->validates()) {
				if ($this->Post->save($this->request->data)) {
					$this->request->data = null;
					$this->set('formMessage', __('Príspevok bol odoslaný.'));
				}
				else {
					$this->set('formMessage', __('Počas odosielania sa vyskytli technické problémy. Skúste prosím ešte raz.'));
				}
			}
		}

		$this->render('/Elements/addPostForm');
	}

	public function admin_index(){
		$publishedPosts = $this->Post->find('all', 
			array('order'=> array('Post.created'=>'desc'))
		);
		$this->set('posts', $publishedPosts);
	}


	public function admin_view($id = null){
		if (!$id) {
			throw new NotFoundException(__('Invalid post'));
		}

		$post = $this->Post->findById($id);
		if (!$post) {
			throw new NotFoundException(__('Invalid post'));
		}
		$this->set('post', $post);
		$this->set('post_id', $post['Post']['id']);
	}


	public function admin_setPublished($id, $published){
		debug($published);
		if ($this->request->is('post')) {
			$this->Post->id = $id;
			$this->Post->saveField('published', $published);
		}
		return $this->redirect(array('action' => 'index'));
	}


	public function admin_delete($id) {
	    if ($this->request->is('get')) {
	        throw new MethodNotAllowedException();
	    }

		if ($this->Post->delete($id)) {
			$this->Session->setFlash(
				__('The post with id: %s has been deleted.', h($id))
			);
		}else {
			$this->Session->setFlash(
				__('The post with id: %s could not be deleted.', h($id))
			);
		}
		return $this->redirect(array('action' => 'index'));
	}



}
